'''
Created on 23/09/2012

@author: Charles Murray
'''

from Parser import *

class StructuralModel(object):
    '''
    classdocs
    '''


    def __init__(self, filename):
        '''
        Constructor
        '''
        self.parser = Parser(filename)
        self.model = self.parser.parse(self.parser.getRoot())
        
    
    def dataParser(self, stream):
        self.stream = stream
        self.stream = self.stream.split(self.model.getStart(), 1)[1]
        return self.model
        
    """
    For root we have something special but everything else
    will go through here. In this if we have two items that 
    have the same index then we use the first found.
    """
    def findData(self, node):
        currentIndex = self.stream.index(node.getSubtree().getFirst().getStart())
        currentItem = node.getSubtree().getFirst()
        for item in node.getSubtree():
            if self.stream.index(item.getStart()) < currentIndex:
                currentIndex = self.stream.index(item.getStart())
                currentItem = item
        return currentItem
                
        
    def getParser(self):
        return self.parser